package com.we823.project.common.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public abstract class ExcelUtil<T extends Serializable> {
	public boolean createExcel2003(String filename, String sheetname,
			Object content) {
		@SuppressWarnings("resource")
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet sheet = workbook.createSheet(sheetname);
		HSSFRow row = sheet.createRow(0);
		HSSFCell cell = row.createCell(0);
		cell.setCellValue(content.toString());

		File file = new File(filename);
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(file);
			workbook.write(fileOutputStream);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return true;
	}

	public XSSFCellStyle getHeaderCellStyle(XSSFWorkbook workbook) {
		if (workbook != null) {
			XSSFCellStyle headerCellStyle = workbook.createCellStyle();
			XSSFFont font = workbook.createFont();

			// 设置垂直居中
			headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
			headerCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

			headerCellStyle.setBorderTop(BorderStyle.THIN);
			headerCellStyle.setBorderBottom(BorderStyle.THIN);
			headerCellStyle.setBorderLeft(BorderStyle.THIN);
			headerCellStyle.setBorderRight(BorderStyle.THIN);

			font.setBold(true);

			headerCellStyle.setWrapText(true);
			headerCellStyle.setFont(font);

			return headerCellStyle;
		}
		return null;
	}

	public boolean createExcel2007(String filename, String sheetname,List<T> content) {
		String[] headers = buildHeader();
		return createExcel2007(filename, sheetname, headers, content);
	}
	public boolean createExcel2007(String filename, String sheetname, String[] headers, List<T> content) {
		XSSFWorkbook workbook = new XSSFWorkbook();
		XSSFSheet sheet = workbook.createSheet(sheetname);

		int headerLength = 0;

		// 设置头信息
		if (headers != null && headers.length>0) {
			XSSFRow header = sheet.createRow(0);
			headerLength = headers.length;
			for (int index = 0; index < headerLength; index++) {
				XSSFCell headerCell = header.createCell(index);
				headerCell.setCellValue(headers[index]);
				headerCell.setCellStyle(getHeaderCellStyle(workbook));
			}
		}

		if (content != null) {
			// list转换成字符串数组
			List<String[]> contentList = parseContent(content);
			// 填充数据
			fillContent(sheet, contentList, headerLength);
		}
		File file = new File(filename);
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(file);
			workbook.write(fileOutputStream);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return true;
	}

	/**
	 * 将list对象转换成string数据
	 * 
	 * @param content
	 * @return
	 */
	public abstract List<String[]> parseContent(List<T> content);
	public abstract String[] buildHeader();

	private void fillContent(XSSFSheet sheet, List<String[]> contentList,
			int headerLength) {
		if (contentList != null) {
			int rowLength = contentList.size();
			int offset = (headerLength>0) ? 1 : 0; //当存在头行时，内容从第2行开始，即1开始
			for (int rowIndex = 0; rowIndex < rowLength; rowIndex++) {
				XSSFRow row = sheet.createRow(rowIndex+offset);
				String[] contents = contentList.get(rowIndex);
				int cellLength = contents.length;
				if(headerLength>0) {
					//当有header设置时，取值由header的长度确定
					if(cellLength>headerLength) {
						cellLength = headerLength;
					}
				}
				for (int cellIndex = 0; cellIndex < cellLength; cellIndex++) {
					XSSFCell cell = row.createCell(cellIndex);
					cell.setCellValue(contents[cellIndex]);
				}

			}
		}
	}
}
